package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.AccountTransfer;
import com.ruoyi.system.service.IAccountTransferService;
import com.ruoyi.web.core.config.CustomDinger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 转出Controller
 * 
 * @author ruoyi
 * @date 2024-03-07
 */
@Controller
@RequestMapping("/system/transfer")
public class AccountTransferController extends BaseController
{
    private String prefix = "system/transfer";

    @Autowired
    private IAccountTransferService accountTransferService;
    @Autowired
    private CustomDinger customDinger;
    @RequiresPermissions("system:transfer:view")
    @GetMapping()
    public String transfer()
    {
        return prefix + "/transfer";
    }

    /**
     * 查询转出列表
     */
    @RequiresPermissions("system:transfer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AccountTransfer accountTransfer)
    {
        startPage();
        List<AccountTransfer> list = accountTransferService.selectAccountTransferList(accountTransfer);
        return getDataTable(list);
    }

    /**
     * 导出转出列表
     */
    @RequiresPermissions("system:transfer:export")
    @Log(title = "转出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AccountTransfer accountTransfer)
    {
        List<AccountTransfer> list = accountTransferService.selectAccountTransferList(accountTransfer);
        ExcelUtil<AccountTransfer> util = new ExcelUtil<AccountTransfer>(AccountTransfer.class);
        return util.exportExcel(list, "转出数据");
    }

    /**
     * 新增转出
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存转出
     */
    @RequiresPermissions("system:transfer:add")
    @Log(title = "转出", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AccountTransfer accountTransfer)
    {
        accountTransfer.setCreateTime(DateUtils.getNowDate());
        return toAjax(accountTransferService.insertAccountTransfer(accountTransfer));
    }

    /**
     * 新增保存转出
     */
    @PostMapping("/outAdd")
    @ResponseBody
    public AjaxResult outAdd(AccountTransfer accountTransfer)
    {
        accountTransfer.setCreateTime(DateUtils.getNowDate());
        AjaxResult ajax = toAjax(accountTransferService.insertAccountTransfer(accountTransfer));
        customDinger.out(accountTransfer.getAccountCode(),accountTransfer.getTransferAccount());
        return ajax;
    }

    /**
     * 修改转出
     */
    @RequiresPermissions("system:transfer:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AccountTransfer accountTransfer = accountTransferService.selectAccountTransferById(id);
        mmap.put("accountTransfer", accountTransfer);
        return prefix + "/edit";
    }

    /**
     * 修改保存转出
     */
    @RequiresPermissions("system:transfer:edit")
    @Log(title = "转出", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AccountTransfer accountTransfer)
    {
        return toAjax(accountTransferService.updateAccountTransfer(accountTransfer));
    }

    /**
     * 删除转出
     */
    @RequiresPermissions("system:transfer:remove")
    @Log(title = "转出", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(accountTransferService.deleteAccountTransferByIds(ids));
    }

    @Log(title = "处理", businessType = BusinessType.OTHER)
    @PostMapping( "/deal")
    @ResponseBody
    public AjaxResult deal(String id)
    {
        AccountTransfer temp = new AccountTransfer();
        temp.setId(Long.parseLong(id));
        temp.setStatus(1);
        return toAjax(accountTransferService.updateAccountTransfer(temp));
    }
}
